package cn.caskm.souvenirshopsystem.config;

import cn.caskm.souvenirshopsystem.domain.Result;
import cn.caskm.souvenirshopsystem.exceptions.BizException;
import cn.caskm.souvenirshopsystem.exceptions.DbException;
import cn.caskm.souvenirshopsystem.exceptions.UnLoginException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(BizException.class)
    public Result<String> handleBizException(RuntimeException exception) {
        String message = exception.getMessage();
        log.error(message);
        return Result.fail(message);
    }

    @ExceptionHandler(DbException.class)
    public Result<String> handleDbException(RuntimeException exception) {
        String message = exception.getMessage();
        log.error(message);
        return Result.fail(message);
    }

    @ExceptionHandler(UnLoginException.class)
    public Result<String> handleUnLoginException() {
        return Result.unLogin();
    }

}
